/**
 * 在一段时间发出多次以后执行func，可以用于click事件模拟双击
 * @param func
 * @param count
 * @param interval
 * @constructor
 */
export function ClickCountInTime<T extends Function>(func: T, count: number = 2, interval: number = 200): T {
  let oldCount = 0
  let startTime = 0
  let timer
  // @ts-ignore
  return function (...args) {
    oldCount++
    if (oldCount === 1) {
      startTime = new Date().getTime();
      if (timer) {
        clearTimeout(timer)
      }
      timer = setTimeout(() => {
        if (oldCount !== 0) {
          oldCount = 0
        }
        clearTimeout(timer)
      }, interval)
    }
    if (oldCount === count && (new Date().getTime() - startTime) < interval) {
      oldCount = 0
      return func.apply(this, args)
    }
  }
}
